package cn.Liujialin.basic.controller;


import cn.Liujialin.basic.util.AjaxResult;
import cn.Liujialin.basic.util.FastDfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import static cn.Liujialin.basic.util.AjaxResult.me;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {
    @PostMapping
    public AjaxResult upload(@RequestPart( "file") MultipartFile file){

        try {

            String Filename = file.getOriginalFilename();
            // xxx.jpg
            String extName = Filename.substring(Filename.lastIndexOf(".")+1);
            String pash =  FastDfsUtil.upload(file.getBytes(), extName);
             //把上传后的路径返回回去
            return AjaxResult.me().setResultObj(pash);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.fail("上传失败!");
        }
    }
    /**
     * 参数：完整路径 /goup1/xxxxx/yyyy
     * 返回值：成功与否，还要返回地址
     *
     */
    @DeleteMapping
    public AjaxResult del(@RequestParam(required = true,value = "path") String path) {
        try {
            String pathTmp = path.substring(1); // goup1/xxxxx/yyyy
            String groupName = pathTmp.substring(0, pathTmp.indexOf("/")); //goup1
            String remotePath = pathTmp.substring(pathTmp.indexOf("/") + 1);// /xxxxx/yyyy
            FastDfsUtil.delete(groupName, remotePath);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败");
        }
        return AjaxResult.fail("成功");
}
}